package org.mirrentools.attendance.console.common;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.filter.logging.Log4j2Filter;
import com.alibaba.druid.pool.DruidDataSource;

import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.spi.DataSourceProvider;

public class DataSourceProviderOfDruid extends DruidDataSource implements DataSourceProvider {
  private static final long serialVersionUID = 381971380983136762L;

  public int maximumPoolSize(DataSource dataSource, JsonObject config) throws SQLException {
    return config.getInteger("druid.maxActive", 20);
  }

  public DataSource getDataSource(JsonObject config) throws SQLException {
    DruidDataSource ds = new DruidDataSource();
    Log4j2Filter log4j2 = new Log4j2Filter();
    ds.getProxyFilters().add(log4j2);
    ds.setUrl(config.getString("druid.url"));
    ds.setDriverClassName(config.getString("druid.driver"));
    ds.setUsername(config.getString("druid.username"));
    ds.setPassword(config.getString("druid.password"));
    ds.setMaxActive(config.getInteger("druid.maxActive", 20));
    ds.setMinIdle(config.getInteger("druid.minIdle", 1));
    ds.setInitialSize(config.getInteger("druid.druid", 1));
    Properties properties = new Properties();
    properties.setProperty("druid.mysql.usePingMethod", "false");
    ds.configFromPropety(properties);
    return ds;
  }

  public void close(DataSource dataSource) throws SQLException {
    if ((dataSource instanceof DataSourceProviderOfDruid))
      ((DataSourceProviderOfDruid) dataSource).close();
  }
}